package com.i72.basic.tools;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;

import java.util.Properties;

public class YAMLHelper {

    public static Properties yamlToProperties(String yamlText){
        try {
            byte[] bytes = yamlText.getBytes("utf-8");
            Resource resource = new ByteArrayResource(bytes);
            return yamlToProperties(resource);
        }catch (Exception e){
            return null;
        }
    }

    public static Properties yamlToProperties(Resource resource){
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource);
        factory.afterPropertiesSet();
        return factory.getObject();
    }

}
